package info

import (
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"github.com/yusufpapurcu/wmi"
	"gopkg.in/ini.v1"
)

type Win32_OnBoardDevice struct {
	Caption              string `json:"说明"`
	CreationClassName    string
	Description          string    `json:"描述"`
	DeviceType           uint16    `json:"设备类型"`
	Enabled              bool      `json:"已启用"`
	HotSwappable         bool      `json:"热交换"`
	InstallDate          time.Time `json:"安装时间"`
	Manufacturer         string    `json:"造商名称"`
	Model                string    `json:"模型"`
	Name                 string    `json:"名称"`
	OtherIdentifyingInfo string    `json:"其他识别信息"`
	PartNumber           string    `json:"部件号"`
	PoweredOn            bool      `json:"通电"`
	Removable            bool      `json:"可移动"`
	Replaceable          bool      `json:"可替换"`
	SerialNumber         string    `json:"序列号"`
	SKU                  string    `json:"库存单位号"`
	Status               string    `json:"状态"`
	Tag                  string    `json:"标识"`
	Version              string    `json:"版本"`
}

func OnBoardInfo() (info []Win32_OnBoardDevice, err error) {
	err1 := wmi.Query("Select * from Win32_OnBoardDevice", &info)
	if err1 != nil {
		return nil, err1
	}

	return info, nil
}

func JsonOnBoardInfo() (f []byte, err error) {
	cpu, err := OnBoardInfo()
	if err != nil {
		return f, err
	}
	t, jsonerr := json.Marshal(cpu)
	if jsonerr != nil {
		return f, jsonerr
	}
	return t, nil

}
func MapOnBoardInfo() (data []map[string]interface{}, err error) {
	//data := make([]map[string]interface{}, 0)
	bus, err := JsonOnBoardInfo()
	if err != nil {
		return data, err
	}
	json.Unmarshal(bus, &data)
	cfg, err1 := ini.Load("./info/onboardconfig.ini")
	if err1 != nil {
		return data, err
	}
	//fmt.Println(cfg.Section(cfg.Section("").Key("理总线类型").String()).Key("1").String())
	list := []string{"设备类型"}
	for _, v := range data {
		for _, vl := range list {
			if a, ok := v[vl].(float64); ok {
				fmt.Println(a)
				v[vl] = cfg.Section(cfg.Section("").Key(vl).String()).Key(strconv.Itoa(int(a))).String()
			} else {
				continue
			}

		}
	}
	return data, nil
}
